import { http } from 'utils'

export async function saveAd(payload = {}) {
  const path = payload.id ? 'advert/update' : 'advert/save'
  try {
    const res = await http.post(path, payload)
    if (res.code === 1) {
      return res.data
    }
    throw res.data
  } catch (err) {
    throw err
  }
}

export async function deleteAdById(id) {
  try {
    const res = await http.get('advert/delete', { id })
    if (res.code === 1) {
      return res
    }
    throw res.info
  } catch (err) {
    throw err
  }
}

export async function getAdList({ page = 1, pageSize = 20 }) {
  try {
    const res = await http.get('advert/list', { page, pageSize })
    if (res.code === 1) {
      return res.data
    }
    throw res.info
  } catch (err) {
    throw err
  }
}
